package com.wb;

import com.wb.fegin.NacosFeignClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Hello world!
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosConsumerApplication {

    private final Logger logger = LoggerFactory.getLogger(NacosConsumerApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerApplication.class, args);
    }

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @RestController
    public class TestController {

//        private final RestTemplate restTemplate;

        @Autowired
        private NacosFeignClient nacosFeignClient;

//        @Autowired
//        public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}

        @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
        public String echo(@PathVariable String str) {
            String result = nacosFeignClient.echo(str);
            return result;
//            return restTemplate.getForObject("http://provide-one/echo/" + str, String.class);
        }

    }

    @RestController
    @RequestMapping("/config")
    @RefreshScope
    public class ConfigController {

        @Value("${cache:helloWorld}")
        private String cache;

        @RequestMapping("/get")
        public String get() {
            return cache;
        }
    }

}